package com.app.control;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * 实用例子
 */
@Controller
public class ExampleController {

    @RequestMapping(value = "/api", method = RequestMethod.GET)
    public String api() {
        System.out.println("swagger-ui.html");
        return "redirect:swagger-ui.html";
    }

    @RequestMapping(value = "/exws")
    public ModelAndView index(String redirect) {
        return new ModelAndView("exws");
    }

    @RequestMapping("testupload")
    public ModelAndView upload(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("name: " + file.getOriginalFilename() + ", size: " + file.getSize());
        // 读取文件内容并写入 指定目录中
        String fileName = file.getOriginalFilename();
        File dest = new File("/tmp/isomp/17/11/10/981387a3-6ce9-4f4f-b004-efbf09e0e3d4/fileLog/" + fileName);
        // 判断目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ModelAndView("ok");
    }


    @RequestMapping(value = "testupload2")
    public ModelAndView upload2(HttpServletRequest request, @RequestParam("uploadFiles") List<MultipartFile> uploadFiles) {
        System.out.println("uploadFiles: " + uploadFiles);
        String fileName = "";

        File file = null;
        InputStream in = null;
//        if (null == uploadFiles) {
//            MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//            MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
//            uploadFiles = multipartRequest.getFiles("uploadFiles");
//        }
        try {

            String saveFileName = "";

            for (MultipartFile uploadFile : uploadFiles) {
                fileName = uploadFile.getOriginalFilename();
                in = uploadFile.getInputStream();
                saveFileName = fileName + "." + System.currentTimeMillis() + ".1";
                file = new File("/tmp/isomp/17/11/10/981387a3-6ce9-4f4f-b004-efbf09e0e3d4/fileLog/", saveFileName);
                FileCopyUtils.copy(uploadFile.getBytes(), file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return new ModelAndView("ok");
    }

    @RequestMapping(value = "testupload3")
    public ModelAndView upload3(HttpServletRequest request, @RequestParam("fileToUpload") List<MultipartFile> uploadFiles) {
        String fileName = "";
        File file = null;
        InputStream in = null;
        System.out.println("request:"+ uploadFiles);
        try {
            String saveFileName = "";
            for (MultipartFile uploadFile : uploadFiles) {
                fileName = uploadFile.getOriginalFilename();
                in = uploadFile.getInputStream();
                saveFileName = fileName + "." + System.currentTimeMillis() + ".1";
                file = new File("/tmp/isomp/17/11/10/981387a3-6ce9-4f4f-b004-efbf09e0e3d4/fileLog/", saveFileName);
                FileCopyUtils.copy(uploadFile.getBytes(), file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return new ModelAndView("ok");
    }

}
